package com.ewei.support.mail.javax;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

import org.budo.support.java.net.util.JavaNetUtil;
import org.budo.support.lang.util.StringUtil;
import org.budo.support.lang.util.SystemUtil;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

import com.ewei.support.mail.AbstractMailReceiver;

import lombok.Getter;
import lombok.Setter;

/**
 * @author li
 * @version 1 (2015年3月19日 上午11:37:59)
 * @since Java7
 */
@Getter
@Setter
public class AbstractJavaxMailReceiver extends AbstractMailReceiver {
    private static final Logger log = Slf4j.getLogger();

    private Boolean deleteMails = false;

    private Integer connectStoreTimeout = 15;

    public int getMode() {
        return this.getDeleteMails() ? Folder.READ_WRITE : Folder.READ_ONLY;
    }

    public void folderOpen(int mode, Folder inbox) throws MessagingException {
        inbox.open(mode);
    }

    public Folder storeGetFolder(Store store, String name) throws MessagingException {
        return store.getFolder(name);
    }

    public Session preapreSession() {
        Properties properties = SystemUtil.getProperties();

        if (this.getSsl()) {
            properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.setProperty("mail.pop3.socketFactory.fallback", "false");
            properties.setProperty("mail.imap.ssl.enable", "true"); // imap.ssl 支持
            properties.setProperty("mail.imap.ssl.trust", "*"); // imap.ssl 支持
        }

        properties.setProperty("mail.pop3.port", getPort() + "");
        properties.setProperty("mail.pop3.socketFactory.port", getPort() + "");
        properties.setProperty("mail.store.protocol", getProtocol());
        // properties.setProperty("mail.stmp.timeout", "" + (10 * 1000)); // mail.stmp.timeout int I/O连接超时时间，单位为毫秒，默认为永不超时
        properties.setProperty("mail.pop3.timeout", "15000");
        properties.setProperty("mail.pop3.connectiontimeout", "15000");
        properties.setProperty("mail.smtp.connectiontimeout", "15000");
        properties.setProperty("mail.smtp.timeout", "15000");
        properties.setProperty("mail.imap.connectiontimeout", "15000");
        properties.setProperty("mail.imap.timeout", "15000");

        return Session.getInstance(properties);
    }

    public boolean checkConfig() {
        String host = this.getHost();
        Integer port = this.getPort();

        if (StringUtil.isEmpty(host) || null == port || port < 0) {
            log.warn("#87 checkConfig() return false, this=" + this);
            return false;
        }

        if (!JavaNetUtil.reachable(host, port)) {
            this.getLogger().error("#92 JavaxMailReceiver mail receive server not reachable this=" + this);
            return false;
        }

        return true;
    }
}